<script setup>
import { ref, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import VideoList from '@/components/VideoList.vue'
import VideoPlayer from '@/components/VideoPlayer.vue'
import { getApiUrl } from '@/config/urlconfig.js'

const route = useRoute()
const folderPath = decodeURIComponent(route.query.path)
const folderId = route.params.id || ''
const isLoading = ref(false)
const error = ref(null)
const folderData = ref(null)

// 初始化空数据
const videos = ref([])
const currentVideo = ref(null)
// console.log(folderPath)

// 从API获取数据
const fetchFolderData = async () => {
  isLoading.value = true
  error.value = null
  // console.log(folderPath)
  try {
    const response = await fetch(getApiUrl('api/videolist', { folder: folderPath }))
    if (!response.ok) {
      throw new Error('获取数据失败')
    }
    
    const data = await response.json()
    if (data.status === 'success') {
      folderData.value = data.data
      
      // 如果有视频，设置当前视频为第一个
      if (data.data.videos && data.data.videos.length > 0) {
        currentVideo.value = {
          video_name: '请选择视频',
          title: '请从左侧列表选择要播放的视频',
          views: 0,
          duration: '0:00',
          folder_path: folderPath,
          is_default: true
        }
      }
    } else {
      throw new Error('获取数据失败')
    }
  } catch (err) {
    error.value = err.message
    console.error('Error fetching folder data:', err)
  } finally {
    isLoading.value = false
  }
}

onMounted(() => {
  fetchFolderData()
})

const handleVideoSelect = (video) => {
  // 构建完整的视频对象，包含文件夹路径信息
  const videoWithPath = {
    ...video,
    folder_path: folderPath // 添加当前文件夹路径
  }
  currentVideo.value = videoWithPath
}
</script>

<template>
  <!-- {{ folderPath }} -->
  <div class="video-collection">
    <div v-if="isLoading" class="loading-state">
      加载中...
    </div>
    <div v-else-if="error" class="error-state">
      {{ error }}
    </div>
    <template v-else-if="folderData && currentVideo">
      <div class="video-player-section">
        <div class="video-player">
          <VideoPlayer :video="currentVideo" :folder-id="folderId" />
        </div>
        <div class="video-info">
          <h1>{{ currentVideo.video_name || currentVideo.title }}</h1>
          <div class="video-stats">
            <span>{{ currentVideo.views || 0 }} 次观看</span>
            <span>{{ currentVideo.duration || '0:00' }}</span>
          </div>
        </div>
      </div>
      <div class="video-list-section">
        <div class="video-list">
          <VideoList
            :folder-id="folderId"
            :folder-data="folderData"
            :current-video="currentVideo"
            @select-video="handleVideoSelect"
          />
        </div>
      </div>
    </template>
    <div v-else class="empty-state">
      <svg class="empty-icon" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
        <path d="M21 12C21 16.9706 16.9706 21 12 21C7.02944 21 3 16.9706 3 12C3 7.02944 7.02944 3 12 3C16.9706 3 21 7.02944 21 12Z" stroke="currentColor" stroke-width="2"/>
        <path d="M9 9H9.01" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
        <path d="M15 9H15.01" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
        <path d="M9.5 15C10.5 14 11.5 14 12 14C12.5 14 13.5 14 14.5 15" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
      </svg>
      <p class="empty-text">这里空空的，和ta跟你的聊天栏一样</p>
      <p class="empty-subtext">请检查文件夹路径是否正确</p>
    </div>
  </div>
</template>

<style scoped>
.video-collection {
  padding: 1rem;
  max-width: 1780px;
  margin: 0 auto;
  margin-top: 80px;
  display: grid;
  grid-template-columns: 3fr 1fr;
  gap: 1rem;
}

@media (max-width: 1024px) {
  .video-collection {
    grid-template-columns: 1fr;
    padding: 0.75rem;
    gap: 0.75rem;
    margin-top: 70px;
  }

  .video-list {
    height: auto;
    max-height: 400px;
  }

  .video-info h1 {
    font-size: 1.25rem;
  }

  .video-stats {
    font-size: 0.8rem;
  }
}

@media (max-width: 768px) {
  .video-collection {
    padding: 0.5rem;
    margin-top: 60px;
  }

  .video-player-section,
  .video-list-section {
    border-radius: 8px;
  }

  .video-info {
    padding: 1rem;
  }

  .video-info h1 {
    font-size: 1.5rem;
    margin: 0 0 0.5rem;
    color: var(--text-color);
  }

  .empty-state {
    padding: 1rem;
    min-height: 200px;
  }

  .empty-icon {
    width: 48px;
    height: 48px;
    margin-bottom: 1rem;
  }

  .empty-text {
    font-size: 1rem;
  }

  .empty-subtext {
    font-size: 0.8rem;
  }
}
.video-player-section {
  background: var(--card-bg);
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 2px 8px var(--card-shadow);
}

.video-player {
  width: 100%;
  background: #000;
  position: relative;
  aspect-ratio: 16/9;
}

.player-placeholder {
  width: 100%;
  height: 100%;
  position: relative;
  cursor: pointer;
}

.player-placeholder img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.play-button {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 60px;
  height: 60px;
  background: rgba(0, 0, 0, 0.7);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 24px;
  transition: all 0.3s ease;
}

.player-placeholder:hover .play-button {
  background: rgba(0, 0, 0, 0.9);
  transform: translate(-50%, -50%) scale(1.1);
}

.video-info {
  padding: 1.5rem;
}

.video-info h1 {
  font-size: 1.5rem;
  margin: 0 0 1rem;
  color: var(--text-color);
}

.video-stats {
  display: flex;
  gap: 1rem;
  color: var(--text-color);
  opacity: 0.7;
  font-size: 0.9rem;
}

.video-list-section {
  background: var(--card-bg);
  border-radius: 12px;
  padding: 1rem;
  box-shadow: 0 2px 8px var(--card-shadow);
}

.video-list-section h2 {
  margin: 0 0 0.75rem;
  color: var(--text-color);
  font-size: 1.25rem;
}

.video-list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  height: calc(100vh - 400px);
  overflow-y: auto;
  padding-right: 8px;
}

@media (max-width: 1024px) {
  .video-list {
    height: calc(100vh - 10px);
    max-height: none;
  }
}

@media (max-width: 768px) {
  .video-list {
    height: calc(100vh - 10px);
  }
}
.video-list::-webkit-scrollbar {
  width: 6px;
}

.video-list::-webkit-scrollbar-track {
  background: var(--card-bg);
  border-radius: 3px;
}

.video-list::-webkit-scrollbar-thumb {
  background: var(--text-color);
  opacity: 0.5;
  border-radius: 3px;
}

.active {
  border: 2px solid #42b883;
}

.loading-state,
.error-state,
.empty-state {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  min-height: 300px;
  background: var(--card-bg);
  border-radius: 12px;
  box-shadow: 0 2px 8px var(--card-shadow);
  color: var(--text-color);
  text-align: center;
  padding: 2rem;
  margin: 2rem auto;
  max-width: 800px;
  animation: fadeIn 0.3s ease-in-out;
}

.empty-icon {
  width: 64px;
  height: 64px;
  margin-bottom: 1.5rem;
  color: var(--text-color);
  opacity: 0.6;
}

.empty-text {
  font-size: 1.25rem;
  font-weight: 500;
  margin: 0 0 0.5rem;
}

.empty-subtext {
  font-size: 0.9rem;
  opacity: 0.7;
  margin: 0;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
</style>